package com.ymttest.business.service.productdata.impl;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.productdata.ActivityOperate;
import com.ymttest.business.service.productdata.bean.ProductActivitysBean;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public abstract class AbstractActivityService implements ActivityOperate{
	private YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
	
	
	@Override
	public ProductActivitysBean addProductInActivitys(String productId) {
		return doAddProductInActivitys(productId);
	}
	
	
	
	protected int getProductInActivityId(String productId,int inaid) throws InterruptedException{
		return ymtProductsInActivityWapper.selectByProductIdAndActivityId(productId,inaid).getIproductinactivityid();
	}
	
	
	protected List<Integer> applyAndApproveProudctInActivitys(int sellerId,String productId, List<Integer> activityIds){
		List<Integer> inaids = new ArrayList<>();
		activityIds.forEach(activityId->{
			try {
				ApplyActivityCall.applyProductInActivity(sellerId, productId,activityId, MapUtil.hashMap());
				int inaid = this.getProductInActivityId(productId,activityId);
				inaids.add(inaid);
				Logger.comment("活动Id:" + activityId);
				Logger.comment("活动商品id: " + inaid);
				
				AdminCallService.approveProductInActivity(String.valueOf(inaid));
				YMTDateUtil.waitTime(1);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return inaids;
		
	}
	
	
	
	
	
	
	//结束活动
	protected void closeProductInActivitys(List<Integer> ProductInActivityIds){
		ProductInActivityIds.forEach(ele->{
			try {
				AdminCallService.closeProductInActivity(String.valueOf(ele));
			} catch (Exception e) {
				Logger.fail(e);
			}
		});
	}
	
	
	
	
	protected abstract ProductActivitysBean doAddProductInActivitys(String productId);
	

}
